<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
		<!--查询代码  开始-->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" id="searchFrm">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">角色名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="roleName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">角色备注</label>
                            <div class="layui-input-inline">
                                <input type="text" name="remark" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn" lay-submit  lay-filter="data-search-btn"><i class="layui-icon">&#xe615;</i> 搜 索</button>
                            <button type="button" class="layui-btn layui-btn-warm" onclick="javascript:$('#searchFrm')[0].reset()" ><i class="layui-icon">&#xe669;</i> 清 空</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
		<!--查询结束  开始-->
	<!--数据表格  开始-->
        <script type="text/html" id="roleTableToolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add">新增角色 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="deletes"> 批量删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="roleTableId" lay-filter="roleTableFilter"></table>
		<div id="test1"></div>
		
		<!-- <table class="layui-hide" id="deptTableId" lay-filter="deptTableFilter"></table> -->

        <script type="text/html" id="roleTableTemplet">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
			<a class="layui-btn layui-btn-xs layui-btn-warm data-count-edit" lay-event="setting">权限设置</a>
        </script>
	    <!--数据表格  结束-->
	
		<!--添加和修改弹出层代码  开始-->
		<div id="addAndUpdateDiv" style="display: none; padding: 0.3125rem;" >
			<form id="dataFrm" lay-filter="dataFrm" method="post" class="layui-form layui-form-pane">
				<div class="layui-form-item">
					<label class="layui-form-label">角色名称</label>
					<div class="layui-input-block">
						<input type="hidden" name="roleId" />
						<input type="text" name="roleName" lay-verify="roleName" autocomplete="off" placeholder="请输入角色名称" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">角色备注</label>
					<div class="layui-input-block">
						<input type="text" name="remark" lay-verify="remark" autocomplete="off" placeholder="请输入备注" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block" style="text-align: center;">
						 <button type="button" class="layui-btn" lay-submit  lay-filter="doSubmit"><i class="layui-icon">&#xe608;</i> 添 加</button>
						 <button type="button" class="layui-btn layui-btn-warm" id="doClose" ><i class="layui-icon">&#x1007;</i> 关 闭</button>
					</div>
				</div>
			</form>
			
			
		</div>
		
		
		<!--添加和修改弹出层代码  结束-->
    </div>
</div>
<script src="../../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../../lib/layui/layui.js" charset="utf-8"></script>
<script src="../../../js/lay-config.js?v=2.0.0" charset="utf-8"></script>
<script src="../../../lib/jquery-3.4.1/jquery.cookie.min.js" charset="utf-8"></script>
<script src="../../../lib/common/common.js" charset="utf-8"></script>

<script>
    layui.use(['form', 'table','treeTable','tree'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
			treeTable=layui.treeTable,
	        tree=layui.tree;
			

        //table渲染数据表格
       var tableIns = table.render({
            elem: '#roleTableId',
            url: api+'sys/role/loadAllRole',
            toolbar: '#roleTableToolbar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox",  fixed: "left",align:"center"},
                {field: 'roleId',  title: 'ID', align:"center"},
                {field: 'roleName',  title: '角色名称'},
                {field: 'createTime',  title: '发布时间',align:"center"},
                {field: 'createUserId',  title: '发布者',align:"center"},
                {title: '操作',  templet: '#roleTableTemplet', fixed: "right", align: "center"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var params=$("#searchFrm").serialize();
            table.reload('roleTableId', {
                page: {
                    curr: 1
                }
                , url: api+'sys/role/loadAllRole?'+params
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(roleTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
               openAddLayer();
            } else if (obj.event === 'deletes') {  // 监听删除操作
               batchDeleteDataByIds();
            }
        });
		
        table.on('tool(roleTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
				openUpdateLayer(obj);
            } else if (obj.event === 'delete') {
               deleteDataById(obj);
            }else if(obj.event==='setting'){
				// alert("设置权限");
				// console.log("设置");
			    settingPermission(obj);
			}
        });
		
		var mainIndex;
		var url;
		//打开添加页面
		function openAddLayer(){
			 mainIndex = layer.open({
			    title: '添加角色',
			    type: 1,  
			    shade: 0.2,
			    maxmin:true,
			    shadeClose: true,
			    area: ['800', '600'],
			    content: $("#addAndUpdateDiv"),
				success:function(){
					url=api+"sys/role/addRole";
					$("#dataFrm")[0].reset();//重置表单
					// editor.txt.html("");//清空富文本
				}
			});
			$(window).on("resize", function () {
			    layer.full(mainIndex);
			});
		}
		//打开修改页面
		function openUpdateLayer(data){
			 mainIndex = layer.open({
			    title: '编辑角色',
			    type: 1,
			    shade: 0.2,
			    maxmin:true,
			    shadeClose: true,
			    area: ['800', '600'],
			    content: $("#addAndUpdateDiv"),
				success:function(){
					 for(let key  in data.data){
					        console.log(key + '---' + data.data[key])
					  }
					// console.log("data:"+data.data);
					url=api+"sys/role/updateRole";
					form.val("dataFrm",data.data);//填充表单
					// $("#dataFrm")[0].reset();//重置表单
					// editor.txt.html(data.content);//设置富文本的初始化内容
				}
			});
			$(window).on("resize", function () {
			    layer.full(mainIndex);
			});
		}
		
		//提交数据
		form.on("submit(doSubmit)",function(data){
		    var params= $("#dataFrm").serialize();
			// var content=editor.txt.html();
			//params+="&content="+content;
			//alert(params);
			$.post(url,{roleId:data.field.roleId,remark:data.field.remark,roleName:data.field.roleName},function(res){
				console.log(res);
				layer.msg(res.msg);
				if(res.code==1){
					tableIns.reload();
				}
				layer.close(mainIndex);
			});
			
			return false;
		});
		
		//根据ID删除
		function deleteDataById(obj){
			 var data = obj.data;
			layer.confirm('真的删除【'+data.roleName+'】这行数据么', function (index) {
				$.post(api+"sys/role/deleteRole",{id:data.roleId},function(res){
					if(res.code==1){
						obj.del();
					} 
					layer.msg(res.msg);
					layer.close(index);
				})
			});
		}
		
		//批量删除
		function batchDeleteDataByIds(){
			layer.confirm('真的删除这些数据么', function (index) {
				var checkStatus = table.checkStatus('roleTableId');
				var data = checkStatus.data;
				var params=""
				$.each(data,function(i,item){
					if(i==0){
						params+="?ids="+item.roleId;
					}else{
						params+="&ids="+item.roleId;
					}
				});
				$.post(api+"sys/role/batchDeleteRole"+params,function(res){
					if(res.code==1){
						tableIns.reload();
					} 
					layer.msg(res.msg);
					layer.close(index);
				})
			});
		}
		var treeIndex;
		//设置权限
		function settingPermission(obj){
			var data = obj.data;
			console.log("权限设置");
			for(let key  in data){
			       console.log(key + '---' + data[key])
			 }
			 treeIndex = layer.open({
			     title: '权限设置',
			     type:1,  
			     shade: 0.2,
			     maxmin:true,
			     shadeClose: true,
			     area: ['500px', '300px'],
			     content: $("#test1"),
			    	success:function(){
			 		// url=api+"sys/role/loadAllMenuByRoleId";
					    //渲染
					    var tableIns = tree.render({
					      elem: $("#test1"),  //绑定元素
					      data:$.ajax({
							 type:"GET",
					     	 url:api+"sys/role/loadAllRole",
							}),
						  shadeClose:true,
						  id:'treeId'
					    });
                       

			 		
			 	}
			 });
			 $(window).on("resize", function () {
			     layer.full(treeIndex);
			 });		

             // layui.close(treeIndex);
			
            
		}
		
		//关闭窗口
		$("#doClose").click(function(){
			layer.close(mainIndex);
		})
		



    });
</script>
<script>

</script>

</body>
</html>